package com.gmadmin.mq.template;

import cn.hutool.json.JSONUtil;
import com.gmadmin.entity.vo.MessageInfoVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 消息通知模板
 *
 * @author zhangliang
 * @version 1.0
 * @date 2022/7/28 11:48
 */
@Service
public class MessageNoticeTemplate {

    // 需要使用topic全称，所以进行topic名称的拼接，也可以自己设置  格式：namespace全称%topic名称
    @Value("${rocketmq.namespace}%${rocketmq.producer-notify.topic}")
    private String topic;

    @Value("${rocketmq.producer-notify.tag}")
    private String tag;

    private final CustomMessageTemplate template;

    public MessageNoticeTemplate(CustomMessageTemplate template) {
        this.template = template;
    }

    /**
     * 同步发送消息
     */

    public void syncSendMessageNotify(@NotNull MessageInfoVo messageInfoVo) {
        messageInfoVo.setKey("id:" + messageInfoVo.getId());
        messageInfoVo.setSource(String.valueOf(messageInfoVo.getType()));
        messageInfoVo.setMessage(JSONUtil.toJsonStr(messageInfoVo));
        template.syncSend(topic, tag, messageInfoVo);
    }
}
